<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <!-- 顶部的文本输入框部分 -->
          <el-input placeholder="请输入内容" v-model="orderList.query">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="orderDataList" stripe border>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column label="是否付款">
          <!-- 此处采用作用域插槽的方式 -->
          <template slot-scope="scope">
            <el-tag size="small" type="danger" v-if="scope.row.order_pay==='0'">未付款</el-tag>
            <el-tag size="small" type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column label="下单时间">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">{{scope.row.create_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <!-- 此处也是作用域插槽 -->
          <template>
            <!-- 修改的按钮 -->
            <el-tooltip content="修改订单地址" placement="top-start" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="editOrderAdress"></el-button>
            </el-tooltip>
            <!-- 物流信息的按钮 -->
            <el-tooltip content="物流信息" placement="top-start" :enterable="false">
              <el-button type="success" icon="el-icon-location" size="mini" @click="wuLiuModel"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页模块 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="orderList.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="orderList.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 修改订单地址的弹出框 -->
    <el-dialog title="修改地址" :visible.sync="editOrderDialogVisible" @close="resetEditAdressAbility">
      <el-form
        :model="editOrderAdressForm"
        :rules="editOrderAdressFormRules"
        ref="editOrderAdressFormRef"
      >
        <el-form-item label="省市区/县" prop="adress1">
          <!-- 渲染一个级联选择框 -->
          <el-cascader v-model="editOrderAdressForm.adress1" :options="cityData"></el-cascader>
        </el-form-item>
        <!-- 详细地址 -->
        <el-form-item label="详细地址" prop="adress2">
          <el-input v-model="editOrderAdressForm.adress2"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="editOrderDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="closeDialog">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 物流信息的弹出框 -->
    <el-dialog title="物流进度呀" :visible.sync="wuLiuDialogVisible">
      <!-- 主体区域 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in wuLiuInfo"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
      <!-- 底部区域 -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="wuLiuDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="closedWuLiuDialog">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入有关地区信息的js文件
import cityData from "./citydata.js";
// 导入物流信息的进度模块
import kuaidi from "./kuaidi";
// 导入物流进度相关的css文件
export default {
  data() {
    return {
      // 订单列表的数据源
      orderList: {
        query: "",
        pagenum: 1,
        pagesize: 100,
      },
      //订单表格的数据源
      orderDataList: [],
      //   总条数
      total: 0,
      //   控制修改地址弹出框的显示与隐藏
      editOrderDialogVisible: false,
      //修改地址弹出框的数据
      editOrderAdressForm: {
        adress1: [],
        adress2: "",
      },
      //   表单的验证规则
      editOrderAdressFormRules: {
        //   省市区/县的验证
        adress1: [
          { required: true, message: "请输入订单地址", trigger: "blur" },
        ],
        adress2: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
      //  定义级联选择框的数据源  根据调入的地区的相关信息
      cityData: cityData,
      // 控制物流信息的弹出框的显示与隐藏
      wuLiuDialogVisible: false,
      //   物流进度的数据源
      wuLiuInfo: [],
    };
  },
  methods: {
    //获取订单列表的数据接口函数
    getOrderList: async function () {
      //  调接口
      const { data: result } = await this.$http.get(`orders`, {
        params: this.orderList,
      });

      if (result.meta.status !== 200) {
        return this.$message.error("获取订单列表失败!");
      }

      this.$message.success(result.meta.msg);

      //   完成赋值操作
      this.orderDataList = result.data.goods;
      this.total = result.data.total;
      console.log(this.orderDataList);
    },
    // 监听分页pagesize的改变的事件
    handleSizeChange: function (pagesize) {
      this.orderList.pagesize = pagesize;
      this.getOrderList();
    },
    // 监听分页pagenum的改变的事件
    handleCurrentChange: function (pagenum) {
      this.orderList.pagenum = pagenum;
      this.getOrderList();
    },
    // 点击修改订单地址的相关处理函数
    editOrderAdress: function () {
      this.editOrderDialogVisible = true;
    },
    // 修改订单地址弹出框的重置功能
    resetEditAdressAbility: function () {
      this.$refs.editOrderAdressFormRef.resetFields();
    },
    // 关闭修改地址的弹出框
    closeDialog: function () {
      this.editOrderDialogVisible = false;
    },
    // 点击按钮，弹出物流会话框
    wuLiuModel: function () {
      this.wuLiuDialogVisible = true;
      //这里不用调用接口,通过引入的js文件来模拟接口的形式

      const { data: res } = kuaidi;

      if (res.meta.status !== 200) {
        return this.$$message.error("获取物流进度失败!");
      }

      this.$message.success(res.meta.message);

      //   完成赋值操作
      this.wuLiuInfo = res.data;

      console.log(res);
    },
    // 关闭物流进度的弹出框
    closedWuLiuDialog: function () {
      this.wuLiuDialogVisible = false;
    },
  },
  created: function () {
    //页面一加载就调用获取订单列表的函数
    this.getOrderList();
  },
};
</script>


<style lang="less" scoped>
@import "../../plugins/timeline/timeline.css";
@import "../../plugins/timeline-item/timeline-item.css";
.el-cascader {
  width: 100%;
}
</style>